package particles;
import java.awt.Image;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;


public class Particle implements ActionListener {
	public static double GRAVITY = .01;
	double x;
	double y;
	double xVel;
	double yVel;
	long time;
	long id;
	BufferedImage myImage;
	static long currID;
	static BufferedImage[] bi;
	static{
		bi = new BufferedImage[12];
		for(int i = 0; i < 12; ++i){
			try{
				bi[i] = ImageIO.read(new File("res/sc-snowflakes"+i+".png"));
			} catch(IOException e){
				System.out.println(e.getMessage());
			}
		}
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		long delT = System.currentTimeMillis() - time;
		time = System.currentTimeMillis();
		//yVel += GRAVITY * delT;
		//y += yVel * delT;
		//x += xVel * delT;
		yVel += GRAVITY;
		y += yVel;
		x += xVel;
	}

	public Particle(double startX, double startY, double velX, double velY){
		Random r  = new Random();
		x = startX;
		y = startY;
		xVel = velX/50.0 * r.nextDouble();
		yVel = velY/50.0 * r.nextDouble();
		id = currID++;
		myImage = bi[(int)id% 12];
	}

	public double getx(){
		return x;
	}

	public double gety(){
		return y;
	}

	public Shape getShape(){
		return new Rectangle2D.Double(x, y, 5, 5);
	}

	public Image getImage(){
		return myImage;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}



	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Particle other = (Particle) obj;
		if (id != other.id)
			return false;
		return true;
	}
	public static final int EXCESS = 100;
	public boolean isInBounds(int xbound, int ybound){
		return y <= ybound + EXCESS && y >= -EXCESS && x <= xbound +EXCESS && x >= -EXCESS;
	}

}
